<?php

namespace app\api\validate;

use app\common\model\Users;
use think\Validate;

class BaseValidate extends Validate
{
    protected $regex = [
        'mobile'                =>      '/1[3456789]\d{9}$/',
        'idcard'                =>      '/^(\d{15}|\d{18}|\d{17}x)$/i',
        'bank_card'             =>      '/^(\d{16}|\d{17}|\d{19})$/',
    ];
    //检查手机号是否注册、是否被锁定
    protected function checkUser($value, $rule, $data){
        $user=Users::where('mobile',$data['mobile'])->find();
        if (empty($user)){
            return '账号不存在';
        }
        if (encrypt($value)!=$user['password']){
            return '账号或密码错误';
        }
        if ($user['is_lock']==1){
            return '账号异常已被锁定';
        }
        return true;
    }
    //检查手机号
    protected function checkMobile($value, $rule, $data)
    {
        return check_mobile($value);
    }
    //检查手机号是否注册
    protected function checkReg($value, $rule, $data){
        $user=Users::where('mobile',$value)->find();
        if (empty($user)){
            return '手机号码未注册';
        }
        return true;
    }
    //检查推荐人
    protected function checkReid($value, $rule, $data)
    {
        $user=Users::where('mobile',$value)->find();
        if (empty($user)){
            return '推荐人不存在';
        }
        if ($user['is_lock']==1){
            return '推荐人状态异常';
        }
        if ($user['mobile']==$data['mobile']){
            return '推荐人不能为自己';
        }
        return true;
    }
    //检查支付宝账号
    protected function checkZfb($value, $rule, $data)
    {
        if(!preg_match('/^0?(13|14|15|16|17|18|19)[0-9]{9}$/',$value) && !preg_match("/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i",$value)){
            return false;
        }
        return true;
    }
    //检查验证码
    protected function checkCode($value, $rule, $data)
    {
        /********************{如果没有设置交易密码 则后台开启了短信验证也无效}********************/
        if($data['is_set_paypwd']==0){
            return true;
        }
        if (empty($rule)){
            $sms_enable=1;
        }else{
            $sms_enable=getSysConfig('sms.'.$rule);
        }
        if ($sms_enable){
            $code=yz_sms_code($data['mobile'],$value);
            if ($code['code']==200){
                return true;
            }else{
                return $code['msg'];
            }
        }else{
            return true;
        }
    }
}
